package study.test.demo.dom4j;

import java.io.FileInputStream;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;
import java.util.List;

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;

/**
 * Created by zfh on 16-4-18.
 */
public class ChannelFileReader2 {
    private FileInputStream fileIn;
    private ByteBuffer byteBuf;
    private long fileLength;
    private int arraySize;
    private byte[] array;
    static int i = 0;
    String eleName = "";

    public ChannelFileReader2(String fileName, int arraySize) throws IOException {
        this.fileIn = new FileInputStream(fileName);
        this.fileLength = fileIn.getChannel().size();
        this.arraySize = arraySize;
        this.byteBuf = ByteBuffer.allocate(arraySize);
    }

    public int read() throws IOException, DocumentException {
        FileChannel fileChannel = fileIn.getChannel();
        int bytes = fileChannel.read(byteBuf);// 读取到ByteBuffer中
        if (bytes != -1) {
            array = new byte[bytes];// 字节数组长度为已读取长度
            byteBuf.flip();
            byteBuf.get(array);// 从ByteBuffer中得到字节数组
            eleName = new String(array, "UTF-8");
            System.out.println("读取的字符串===" + eleName);
            System.err.println("读取的字符串长度===" + eleName.length());
            eleName = StringUtils.substringBetween(eleName, "<RECORD>", "</RECORD>");
            System.out.println("之后===" + eleName);
            System.out.println("====处理====");
            // 然后只读取</ >里面的
            eleName = "<RECORD>" + eleName + "</RECORD>";
            Document document = DocumentHelper.parseText(eleName);
            Element eleRoot = document.getRootElement();
            List<Element> eleList = eleRoot.elements();
            if (CollectionUtils.isNotEmpty(eleList)) {
                for (Element ele : eleList) {
                    System.out.println(ele.getName() + "==" + ele.getText());
                }
            }
            if (eleName.contains("RECORD")) {
                return -1;
            }
            byteBuf.clear();
            i++;
            return -1;
//            return bytes;
        }
        return -1;
    }

    public void close() throws IOException {
        fileIn.close();
        array = null;
    }

    public byte[] getArray() {
        return array;
    }

    public long getFileLength() {
        return fileLength;
    }

    public static void main(String[] args) throws IOException, DocumentException {
        ChannelFileReader2 reader = new ChannelFileReader2("F:\\home\\test.xml", 1048576);
        long start = System.currentTimeMillis();
        while (reader.read() != -1)
            ;
        long end = System.currentTimeMillis();
        reader.close();
        System.out.println("ChannelFileReader: " + (end - start));
        System.out.println(i);
    }
}
